﻿using System.Collections.Generic;
using System.Linq;

namespace ReactiveGraph.Core
{
    public class ReactiveNodeList : RxCollection<IReactiveNode>
    {
        private readonly Dictionary<string, IReactiveNode> _childNodeMap = new Dictionary<string, IReactiveNode>();

        public IReactiveNode this[string name]
        {
            get { return _childNodeMap[name]; }
        }

        protected override void OnCollectionChanged(ChangedArgs<IReactiveNode> changedArgs)
        {
            base.OnCollectionChanged(changedArgs);
            changedArgs.RemovedItems.Run(n => _childNodeMap.Remove(n.NodeName));
            changedArgs.AddedItems.Run(n => _childNodeMap.Add(n.NodeName, n));
        }
    }
}